package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: jxc
 * @description:
 * @author: SXC
 * @create: 2021-09-04 14:47
 **/
@RestController
@RequestMapping(value = "customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /*
     *请求URL：http://localhost:8080 /customer/list
        请求参数：Integer page, Integer rows, String  customerName
        请求方式：POST
        返回值类型：JSON
        返回值：Map<String,Object>
     *  */

    /**
     *
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping(value = "/list")
    public Map<String,Object> customerList(Integer page, Integer rows, String  customerName){
        List<Customer> list = customerService.customerList(page,rows,customerName);
        int total  = customerService.selectCount();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    /**
     * 2.2、客户添加或修改
     * 请求URL：http://localhost:8080/ customer/save?customerId=1
     * 请求参数：Customer customer
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     * Response Example Value：
     */
    /**
     * 2.2、客户添加或修改
     * @param customer
     * @return
     */
    @PostMapping(value = "/save")
    public ServiceVO save(Customer customer){
        customerService.saveOrUpdateCustomer(customer);
        return  new ServiceVO(100,"请求成功",null);
    }

    /**
     * 2.3、客户删除（支持批量删除）
     * 请求URL：http://localhost:8080/customer/delete
     * 请求参数：String  ids
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping(value = "/delete")
    public ServiceVO deleteList(String[] ids){
        customerService.deleteList(ids);
        return  new ServiceVO(100,"请求成功",null);
    }

}
